package com.hnkywl.sts.action.jsgl;

import com.hnkywl.sts.dao.jsgl.CgjsdDao;
import com.hnkywl.sts.dao.jsgl.CgjsdmxDao;
import com.hnkywl.sts.entity.cggl.Jsd;
import com.hnkywl.sts.entity.organ.Nbgs;
import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.entity.sys.Yg;
import com.hnkywl.sts.service.base.DxpzService;
import com.hnkywl.sts.service.jsgl.CgjsdService;
import com.hnkywl.sts.service.organ.GsjgService;
import com.hnkywl.sts.service.sys.GnczService;
import com.hnkywl.sts.service.sys.YgService;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: lenovo
 * Date: 13-4-23
 * Time: 上午9:10
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/jsgl/cgjsd")
public class CgjsdAction extends SimpleActionSupport {
    private CgjsdService cgjsdService;
    private DxpzService dxpzService;
    private GsjgService gsjgService;
    private CgjsdDao cgjsdDao;
    private CgjsdmxDao cgjsdmxDao;
    //操作类型 add表示新增,update表示修改
    private String operation;
    //分页组件 起始位置
    private int page;
    //分页组件 页面大小
    private int rows;
    //行索引
    private int index;
    //传入的企业机构Id值
    private Integer type;
    //结算单号
    private String jsdh;
    //客户的ID
    private Long glqyId;
    //采购公司ID
    private Long cggsId;
    //公司的ID
    private Long gsId;
    //发票类型ID
    private Long fpzlId;
    //供应商名称
    private String glqyQc;
    //公司的名称
    private String gsJc;
    //申请（查询）开始时间
    private Date sqrqStart;
    //申请（查询）结束时间
    private Date sqrqEnd;
    private Jsd cgjsd;
    //销售结算单的ID
    private Long cgjsdId;
    //申报人的Id
    private Long sqrId;
    //状态
    private Integer zt;

    //供应商ID
    private Long gysid;

    private Yg yg;

    //功能操作
    private List<Gncz> gnczList;
    private GnczService gnczService;

    //采购结算单的查询分页显示
    @Action("cgjsdList")
    public void cgjsdList() {
        this.renderJson(this.cgjsdService.findAllCgjsd(zt, jsdh, sqrqStart, sqrqEnd, glqyId, gsId, gsJc, fpzlId, page, rows));
    }

    @Action("cgjsd")
    public String cgjsd() {
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        return SUCCESS;
    }

    @Actions({@Action("cgjsd_content"),@Action("cgjsd_detail")})
    public String cgjsdContent() {
        yg = YgService.getCurrentYg();
        sqrId = YgService.getCurrentYg().getId();
        gsId = YgService.getCurrentYgZgGsId();
        //进入update页面
        if (cgjsdId != null) {
            cgjsd = cgjsdService.findCgjsdDetail(cgjsdId);
        } else {
            cgjsd = new Jsd();
            cgjsd.setGs((Nbgs) gsjgService.findGsjgDetail(gsId));
        }
        return SUCCESS;
    }

    /**
     * 方法名:     addCgjsd
     * 功能描述:      增加采购结算单
     * 输入参数:
     * 异常:      <RuntimeException  Exception>
     */
    @Action("add_cgjsd")
    public void addCgjsd() {
        try {
            this.cgjsdService.saveCgjsd(cgjsd);
            renderText("{\"statusCode\":\"200\", \"message\":\"添加采购结算单成功！\", \"operationType\":\"add\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 方法名:     updateCgjsd
     * 功能描述:      修改 采购结算单
     * 输入参数:
     * 异常:      <RuntimeException  Exception>
     */
    @Action("update_cgjsd")
    public void updateCgjsd() {
        try {
            this.cgjsdService.updateCgjsd(cgjsd);
            renderText("{\"statusCode\":\"200\",\"message\":\"修改采购结算单成功！\"}");
        } catch (RuntimeException e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }

    }

    /**
     * 方法名:     deleteCgjsd
     * 功能描述:      删除采购结算单
     * 输入参数:
     * 异常:      <RuntimeException  Exception>
     */
    @Action("delete_cgjsd")
    public void deleteCgjsd() {
        try {
            this.cgjsdService.deleteCgjsd(cgjsdId);
            renderText("{\"statusCode\":\"200\", \"message\":\"删除采购结算单成功！\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    //修改收款审核状态
    @Action("change_status")
    public void changeStatus() {
        try {
            cgjsdService.changeStatus(cgjsdId, zt);
//            Jsd jsd = cgjsdService.findCgjsdDetail(cgjsdId);
//            dxpzService.sendMsgForCgjs(jsd.getJsdh(), jsd.getSqr().getXm(), jsd.getMe().toString());
            renderText("{\"statusCode\":\"200\", \"message\":\"采购结算已确认！\", \"operationType\":\"check\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }

    }

    //采购结算单中表格
    @Action("cgjsd_table")
    public void cgjsdTable() {
        List<Map> cgjsdList=new ArrayList<Map>();
        Jsd jsd = this.cgjsdService.findCgjsdDetail(cgjsdId);
        if (jsd.getShzt() == 1) {
            cgjsdList = this.cgjsdmxDao.findCgjsdmxSearch(cgjsdId);
        } else {
            cgjsdList = this.cgjsdDao.cgjsdList(cgjsdId);
        }

        Double sbsl = 0d;
        Double hse = 0d;
        Double je = 0d;
        Double se = 0d;
        java.text.DecimalFormat df = new java.text.DecimalFormat("0.00");
        java.text.DecimalFormat jtdf = new java.text.DecimalFormat("0.0000");
        StringBuffer sbTable = new StringBuffer();
        if (null != cgjsdList) {
            sbTable.append("<table class=\"tb intb\">");
            sbTable.append("<tr>");
            sbTable.append("<td style='width:110px;'>采购单号</td><td width='110px'>品名</td><td width='45px'>材质</td><td>规格型号</td><td width='40px'>产地</td>" +
                    "<td width='30px'>单位</td><td style='width:70px;'>数量</td><td style='width:60px;'>含税单价(元)</td><td style='width:60px;'>不含税单价(元)</td><td style='width:80px;'>含税总金额(元)</td><td style='width:80px;'>净余额(元)</td><td style='width:80px;'>税额(元)</td>");
            sbTable.append("</tr>");
            for (int i = 0; i < cgjsdList.size(); i++) {
                sbTable.append("<tr>");

                sbTable.append("<td>");
                if (null != cgjsdList.get(i).get("cgdh")) {
                    sbTable.append(cgjsdList.get(i).get("cgdh"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != cgjsdList.get(i).get("wzgg.name")) {
                    sbTable.append(cgjsdList.get(i).get("wzgg.name"));
                }
                sbTable.append("</td>");
                sbTable.append("<td>");
                if (null != cgjsdList.get(i).get("wzgg.cz")) {
                    sbTable.append(cgjsdList.get(i).get("wzgg.cz"));
                }
                sbTable.append("</td>");
                sbTable.append("<td>");
                if (null != cgjsdList.get(i).get("wzgg.ggxh")) {
                    sbTable.append(cgjsdList.get(i).get("wzgg.ggxh"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != cgjsdList.get(i).get("cd.z")) {
                    sbTable.append(cgjsdList.get(i).get("cd.z"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != cgjsdList.get(i).get("wzgg.dw")) {
                    sbTable.append(cgjsdList.get(i).get("wzgg.dw"));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != cgjsdList.get(i).get("rksl")) {
                    sbTable.append(jtdf.format(cgjsdList.get(i).get("rksl")));
                    sbsl += (Double) cgjsdList.get(i).get("rksl");
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != cgjsdList.get(i).get("hsj")) {
                    sbTable.append(df.format(cgjsdList.get(i).get("hsj")));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != cgjsdList.get(i).get("bhsj")) {
                    sbTable.append(df.format(cgjsdList.get(i).get("bhsj")));
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != cgjsdList.get(i).get("se") && null != cgjsdList.get(i).get("je")) {
                    Double mdse = (Double) cgjsdList.get(i).get("se");
                    Double mdje = (Double) cgjsdList.get(i).get("je");
                    Double cgjsdme = (Double) cgjsdList.get(i).get("me");
                    Double h_se = mdse + mdje;
                    sbTable.append(df.format(h_se));
                    hse += h_se;
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != cgjsdList.get(i).get("je")) {
                    sbTable.append(df.format(cgjsdList.get(i).get("je")));
                    je += Double.valueOf(cgjsdList.get(i).get("je").toString());
                }
                sbTable.append("</td>");

                sbTable.append("<td>");
                if (null != cgjsdList.get(i).get("se")) {
                    sbTable.append(df.format(cgjsdList.get(i).get("se")));
                    se += Double.valueOf(cgjsdList.get(i).get("se").toString());
                }
                sbTable.append("</td>");

                sbTable.append("</tr>");
            }
            sbTable.append("<tr>");

            sbTable.append("<th colspan=\"6\">合计：");
            sbTable.append("</th>");

            sbTable.append("<td>");
            sbTable.append(jtdf.format(sbsl));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append(df.format(hse));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append(df.format(je));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append(df.format(se));
            sbTable.append("</td>");

            sbTable.append("</tr>");
            sbTable.append("</table>");
        }
        renderHtml(sbTable.toString());
    }


    @Action("cgd_search")
    public String cgd_search() {
        return SUCCESS;
    }
    //===========getter===setter========================

    public String getOperation() {
        return operation;
    }

    public void setOperation(String operation) {
        this.operation = operation;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public String getJsdh() {
        return jsdh;
    }

    public void setJsdh(String jsdh) {
        this.jsdh = jsdh;
    }

    public Long getGlqyId() {
        return glqyId;
    }

    public void setGlqyId(Long glqyId) {
        this.glqyId = glqyId;
    }

    public Date getSqrqEnd() {
        return sqrqEnd;
    }

    public void setSqrqEnd(Date sqrqEnd) {
        this.sqrqEnd = sqrqEnd;
    }

    public Date getSqrqStart() {
        return sqrqStart;
    }

    public void setSqrqStart(Date sqrqStart) {
        this.sqrqStart = sqrqStart;
    }

    public String getGsJc() {
        return gsJc;
    }

    public void setGsJc(String gsJc) {
        this.gsJc = gsJc;
    }

    public String getGlqyQc() {
        return glqyQc;
    }

    public void setGlqyQc(String glqyQc) {
        this.glqyQc = glqyQc;
    }

    public Long getFpzlId() {
        return fpzlId;
    }

    public void setFpzlId(Long fpzlId) {
        this.fpzlId = fpzlId;
    }

    public Long getCggsId() {
        return cggsId;
    }

    public void setCggsId(Long cggsId) {
        this.cggsId = cggsId;
    }

    public Long getGsId() {
        return gsId;
    }

    public void setGsId(Long gsId) {
        this.gsId = gsId;
    }

    public Jsd getCgjsd() {
        return cgjsd;
    }

    public void setCgjsd(Jsd cgjsd) {
        this.cgjsd = cgjsd;
    }

    public Long getCgjsdId() {
        return cgjsdId;
    }

    public void setCgjsdId(Long cgjsdId) {
        this.cgjsdId = cgjsdId;
    }

    public Long getSqrId() {
        return sqrId;
    }

    public void setSqrId(Long sqrId) {
        this.sqrId = sqrId;
    }

    public Integer getZt() {
        return zt;
    }

    public void setZt(Integer zt) {
        this.zt = zt;
    }

    public Long getGysid() {
        return gysid;
    }

    public void setGysid(Long gysid) {
        this.gysid = gysid;
    }

    public List<Gncz> getGnczList() {
        return gnczList;
    }

    public void setGnczList(List<Gncz> gnczList) {
        this.gnczList = gnczList;
    }

    public Yg getYg() {
        return yg;
    }

    public void setYg(Yg yg) {
        this.yg = yg;
    }

    @Autowired
    public void setCgjsdService(CgjsdService cgjsdService) {
        this.cgjsdService = cgjsdService;
    }

    @Autowired
    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }

    @Autowired
    public void setDxpzService(DxpzService dxpzService) {
        this.dxpzService = dxpzService;
    }

    @Autowired
    public void setCgjsdDao(CgjsdDao cgjsdDao) {
        this.cgjsdDao = cgjsdDao;
    }

    @Autowired
    public void setCgjsdmxDao(CgjsdmxDao cgjsdmxDao) {
        this.cgjsdmxDao = cgjsdmxDao;
    }
    @Autowired
    public void setGsjgService(GsjgService gsjgService) {
        this.gsjgService = gsjgService;
    }
}
